
import 'package:bike_ride/common/util/hysize_fit.dart';
import 'package:bike_ride/common/widget/xb_button.dart';
import 'package:bike_ride/generated/l10n.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

///[公共弹窗]
///[onWillPopBol]是否响应返回键事件
///[barrierDismissible]点击周围是否消失
showAlertDialog({
  required BuildContext context,
  String? title,
  String? contentTxt,
  Widget? content,
  bool hasCancel = true,
  bool hasConfirm = true,
  bool barrierDismissible = false,
  String? confirmName,
  String? cancelName,
  Function? onCancel,
  Function? onConfirm,
  bool onWillPopBol = true,
}) {
  if(!context.mounted) return;
  showDialog(
      context: context,
      useRootNavigator: true,
      barrierDismissible: barrierDismissible,
      builder: (context) =>
          WillPopScope(child: MyDialog(
              title: title,
              content: contentTxt != null ? Text(contentTxt,style: TextStyle(
                fontSize: 28.rpx,
                color: Colors.black87,
                fontWeight: FontWeight.w500,
              ),) :content,
              hasCancel: hasCancel,
              hasConfirm: hasConfirm,
              confirmName: confirmName,
              cancelName: cancelName,
              onConfirm: onConfirm,
              onCancel: onCancel), onWillPop: () async => onWillPopBol));
}

class MyDialog extends Dialog {
  final String? title;
  final Widget? content;
  final Function? onCancel;
  final Function? onConfirm;
  final bool hasCancel;
  final bool hasConfirm;
  final String? confirmName;
  final String? cancelName;
  final double? width;

  const MyDialog({
    Key? key,
    this.title,
    this.content,
    this.onCancel,
    this.onConfirm,
    this.hasCancel = true,
    this.hasConfirm = true,
    this.confirmName,
    this.cancelName,
    this.width,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final mCancelName = cancelName??S.current.cancel;
    final mConfirmName = confirmName??S.current.confirm;
    final mTitle = title??S.current.dialogTitle;

    return Center(
      child: Container(
        width: width ?? MediaQuery.of(context).size.width * 0.8,
        padding: EdgeInsets.symmetric(horizontal: 40.rpx, vertical: 30.rpx),
        decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.all(Radius.circular(16.rpx))),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            if (mTitle.isNotEmpty)
              Padding(
                padding: EdgeInsets.only(bottom: 10.rpx),
                child: Align(
                  alignment: Alignment.center,
                  child: Text(mTitle,
                      style: TextStyle(
                        fontSize: 34.rpx,
                        height: 1.2,
                        color: Colors.black,
                        fontWeight: FontWeight.bold,
                      )),
                ),
              ),
            Container(
              padding: EdgeInsets.symmetric(vertical: 30.rpx),
              width: double.infinity,
              child: DefaultTextStyle(
                  style: TextStyle(fontSize: 30.rpx, color: Colors.black87),
                  textAlign: TextAlign.center,
                  child: content ?? Container()),
            ),
            Padding(
              padding: EdgeInsets.only(top: 10.rpx),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  !!hasCancel
                      ? Container(
                          margin: EdgeInsets.only(right: 30.rpx),
                          child: XbButton(
                            isDefault: true,
                            height: 75.rpx,
                            onPressed: () {
                              if (onCancel != null) {
                                onCancel!();
                              } else {
                                Navigator.pop(context);
                              }
                            },
                            width: 220.rpx,
                            child: Text(mCancelName),
                          ))
                      : const SizedBox.shrink(),
                  hasConfirm
                      ? XbButton(
                          onPressed: () {
                            if (onConfirm != null) {
                              onConfirm!();
                            }
                          },
                          height: 75.rpx,
                          width: 220.rpx,
                          child: Text(mConfirmName),
                        )
                      : const SizedBox.shrink()
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
